home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / hardware / video05 / v_access.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-05  |  23.5 KB  |  1,012 lines

  1. /****************************************************************************/
  2. /* v_access.c  version 0.5                                                  */
  3. /*                                                                          */
  4. /* routines to access the video-blaster and compatible cards                */
  5. /* for the GNU djgpp/gcc compiler                                           */
  6. /*                                                                          */
  7. /* (C)1994 Bernhard Schwall                                                 */
  8. /*   based on routines from Chips & Technologies                            */
  9. /*   published in TOOL 02/92                                                */
  10. /*                                                                          */
  11. /* version 0.1                                                              */
  12. /*   main routines to display the video under dos and linux                 */
  13. /*                                                                          */
  14. /* version 0.2                                                              */
  15. /*   access the videomemory under dos and linux                             */
  16. /*                                                                          */
  17. /* version 0.3                                                              */
  18. /*   disable V_SetVolume if not Media Pro Plus                              */
  19. /*   support of SB16-mixer                                                  */
  20. /*                                                                          */
  21. /* version 0.5                                                              */
  22. /*   disable every memory access if MemAccess isn't defined (required for   */
  23. /*   Linux with >= 16 MB ram)                                               */
  24. /*                                                                          */
  25. /****************************************************************************/
  26.  
  27. #include "v_access.h"
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #ifdef linux
  32. #include <unistd.h>
  33. #include <sys/types.h>
  34. #include <sys/mman.h>
  35. #include <fcntl.h>
  36. #endif
  37.  
  38.  
  39. static WORD Einstellung[10] = { 77, 31, 14, 30, 21,  3,  0,  0,  0,  0 };
  40.  
  41. static BYTE FarbEinstellung[7] = { 34, 26, 18, 32, 32, 32, 32 };
  42.  
  43. static WORD Lautstaerke, VideoAddress;
  44. static INT  WindowPosX, WindowPosY;
  45. static WORD WindowSizeX, WindowSizeY, ColorKey, WriteProtectMask,
  46.             VideoSource, InputFormat, HorizontalZoom, VerticalZoom;
  47. static BOOL Flag_TrueColor, Flag_HiColor, Flag_3_4, Freeze;
  48. WORD PortNr;
  49. BYTE *VidMem;
  50.  
  51. #ifdef linux
  52. static INT Mem_descriptor;
  53. #endif
  54. /****************************************************************************/
  55.  
  56. void _regwrit(WORD Wert_X, WORD Wert_Y)
  57. {
  58.   if (Wert_Y > 1) return;
  59.   port_out(24,PortNr);
  60.   port_out((port_in(PortNr+1) & (~(1 << Wert_Y))) | 
  61.            ((Wert_X & 1) << Wert_Y), PortNr+1);
  62. }
  63.  
  64. WORD _regread(WORD Wert_X)
  65. {
  66.   port_out(24,PortNr);
  67.   if (Wert_X > 2) return 0;
  68.   return ((port_in(PortNr+1) >> Wert_X) & 1);
  69. }
  70.  
  71. WORD _i2c_ack()
  72. {
  73.   WORD Result;
  74.  
  75.   _regwrit(1, 1);
  76.   _regwrit(1, 0);
  77.   Result = _regread(2);
  78.   _regwrit(0, 0);
  79.   return Result;
  80. }
  81.  
  82. void _i2c_start()
  83. {
  84.   _regwrit(1, 1);
  85.   _regwrit(1, 0);
  86.   _regwrit(0, 1);
  87.   _regwrit(0, 0);
  88. }
  89.  
  90. void _i2c_stop()
  91. {
  92.   _regwrit(0, 1);
  93.   _regwrit(1, 0);
  94.   _regwrit(1, 1);
  95. }
  96.  
  97. WORD _i2c_wdata(WORD Wert_X, WORD Wert_Y)
  98. {
  99.   WORD h;
  100.  
  101.   if (Wert_Y == 0)
  102.     _i2c_start();
  103.   for (h=0; h<8; h++){
  104.     _regwrit(Wert_X >> 7, 1);
  105.     _regwrit(1, 0);
  106.     _regwrit(0, 0);
  107.     Wert_X <<= 1;
  108.   }
  109.   return (_i2c_ack());
  110. }
  111.  
  112. WORD read_i2c()
  113. {
  114.   WORD h, i;
  115.  
  116.   i = 0;
  117.   for (h=0; h<8; h++){
  118.     _regwrit(1, 0);
  119.     i = (i << 1) | _regread(2);
  120.     _regwrit(0, 0);
  121.   }
  122.   return i;
  123. }
  124.  
  125. void setvideoheight(WORD MaxHoehe)
  126. {
  127.   WORD h;
  128.  
  129.   port_out(36,PortNr);
  130.   h = port_in(PortNr + 1);
  131.   port_out(37,PortNr);
  132.   h += (((WORD)port_in(PortNr + 1)) & 3) << 8;
  133.   h += MaxHoehe;
  134.   port_outw((h & 0xff00) + 41,PortNr);
  135.   port_outw((h << 8) + 40,PortNr);
  136. }
  137.  
  138. void scale_video(WORD SizeX, WORD SizeY)
  139. {
  140.   WORD x, y, h, MaxHoehe;
  141.  
  142.   x = SizeX;
  143.   if (Flag_TrueColor)
  144.     x = (x * 3) >> 1;
  145.   else {
  146.     if (!Flag_HiColor)
  147.       x <<= 1;
  148.   }
  149.   x <<= 5;
  150.   if (x % 680 > 0)
  151.     x = x / 680 + 1;
  152.   else
  153.     x /= 680;
  154.   if (InputFormat == 1)
  155.     MaxHoehe = 480;
  156.   else
  157.     MaxHoehe = 572;
  158.   setvideoheight(MaxHoehe);
  159.   y = SizeY << 6;
  160.   if (y % MaxHoehe > 0)
  161.     y = y / MaxHoehe + 1;
  162.   else
  163.     y /= MaxHoehe;
  164.   port_out(33,PortNr);
  165.   h = port_in(PortNr + 1) & 0xf3;
  166.   if (x < 64) h |= 4;
  167.   if (y < 64) h |= 8;
  168.   port_out(h,PortNr + 1);
  169.   port_out(32,PortNr);
  170.   h = port_in(PortNr + 1) & 0xfb;
  171.   if ((y & 0xff) <= 32)
  172.     h |= 4;
  173.   port_out(h,PortNr + 1);
  174.   if (x < 64) port_outw((x << 8) + 45,PortNr);
  175.   if (y < 64) {
  176.     port_outw((y << 8) + 46, PortNr);
  177.     port_outw((y << 8) + 47, PortNr);
  178.   }
  179. }
  180.  
  181. void setdisplaywindow(INT PosX, INT PosY, WORD SizeX, WORD SizeY)
  182. {
  183.   WORD HPosX, HPosY;
  184.   INT  HSizeX, HSizeY;
  185.  
  186.   HPosX = PosX + Einstellung[0];
  187.   HPosY = PosY + Einstellung[1];
  188.   port_outw((HPosX << 8) + 65, PortNr);
  189.   port_outw((HPosX & 0xff00) + 66, PortNr);
  190.   port_outw((HPosY << 8) + 67, PortNr);
  191.   port_outw((HPosY & 0xff00) + 68, PortNr);
  192.   HSizeX = SizeX + HPosX - 1;
  193.   HSizeY = SizeY + HPosY - 1;
  194.   port_outw((HSizeX << 8) + 69, PortNr);
  195.   port_outw((HSizeX & 0xff00) + 70, PortNr);
  196.   port_outw((HSizeY << 8) + 71, PortNr);
  197.   port_outw((HSizeY & 0xff00) + 72, PortNr);
  198. }
  199.  
  200. void setdisplayposition(INT PosX, INT PosY)
  201. {
  202.   INT  HPosX, HPosY;
  203.   WORD c, b, d;
  204.  
  205.   HPosX = (~PosX) + 1;
  206.   c = HorizontalZoom+1;
  207.   if (Flag_3_4) c++;
  208.   HPosX = ((HPosX >> c) - Einstellung[2]) & 0x1fe;
  209.   d = (HPosX >> 8) & 1;
  210.   port_outw((HPosX << 8) + 73, PortNr);
  211.   HPosY = (~PosY) + 1;
  212.   HPosY = (HPosY >> VerticalZoom) - Einstellung[3];
  213.   b = (((HPosY >> 8) << 4) & 16) | d;
  214.   port_outw((HPosY << 8) + 74, PortNr);
  215.   port_outw((b << 8) + 75, PortNr);
  216. }
  217.  
  218. void fitvideo()
  219. {
  220.   BOOL f = Freeze;
  221.   if (f == FALSE) V_FreezeVideo();
  222.   scale_video(WindowSizeX, WindowSizeY);
  223.   if (Flag_TrueColor)
  224.     setdisplayposition((WindowPosX * 3) >> 1, WindowPosY);
  225.   else
  226.     setdisplayposition(WindowPosX, WindowPosY);
  227.   if (f == FALSE) V_UnfreezeVideo();
  228.   if (Flag_TrueColor)
  229.     setdisplaywindow((WindowPosX * 3) >> 1, WindowPosY,
  230.              (WindowSizeX * 3) >> 1, WindowSizeY);
  231.   else
  232.     setdisplaywindow(WindowPosX, WindowPosY, WindowSizeX, WindowSizeY);
  233.   scale_video(WindowSizeX, WindowSizeY);
  234. }
  235.  
  236. void UpdateVideoWindow()
  237. {
  238.   V_DisableFieldReplication();
  239.   if (HorizontalZoom > 0)
  240.     V_HorizontalZoom(HorizontalZoom);
  241.   if (VerticalZoom > 0)
  242.     V_VerticalZoom(VerticalZoom);
  243.   fitvideo();
  244. }
  245.  
  246. /*----------------------------------------------------------------------*/
  247.  
  248. WORD V_wr_i2c(WORD Nr, WORD Index, WORD Wert)
  249. {
  250.   if (_i2c_wdata(Nr, 0) != 0) {
  251.     _i2c_stop();
  252.     return 0;
  253.   }
  254.   if (_i2c_wdata(Index, 1) != 0) {
  255.     _i2c_stop();
  256.     return 0;
  257.   }
  258.   if (_i2c_wdata(Wert, 2) != 0) {
  259.     _i2c_stop();
  260.     return 0;
  261.   } else {
  262.     _i2c_stop();
  263.     return 1;
  264.   }
  265. }
  266.  
  267. void V_SetVolume(WORD Wert)
  268. {
  269.   Lautstaerke = (Wert &= 255);
  270. #ifdef MEDIA_PRO_PLUS_MIXER
  271.   V_wr_i2c(0x88, 3, Wert >> 2);
  272. #endif
  273. /* set the mixer of the SB16. Here should /dev/mixer be used if running   */
  274. /* under linux                                                            */
  275. #ifdef SB16_MIXER
  276.   Wert >>= 3;
  277.   port_out(0x38, 0x224);
  278.   port_out(Wert << 3, 0x225);
  279.   port_out(0x39, 0x224);
  280.   port_out(Wert << 3, 0x225);
  281. #endif
  282. }
  283.  
  284. WORD V_GetVolume()
  285. {
  286.   return Lautstaerke;
  287. }
  288.  
  289. void V_EnableVideo()
  290. {
  291.   port_out(64,PortNr);
  292.   port_out(port_in(PortNr + 1) | 3, PortNr+1);
  293. }
  294.  
  295. void V_DisableVideo()
  296. {
  297.   port_out(64, PortNr);
  298.   port_out(port_in(PortNr + 1) & 0xfc, PortNr+1);
  299. }
  300.  
  301. void V_SetColorKey(WORD Wert)
  302. {
  303.   WORD h, i;
  304.  
  305.   ColorKey = Wert;
  306.   if (Wert > 0xf) {
  307.     port_outw((Wert << 8) + 78, PortNr);
  308.     return;
  309.   }
  310.   h = port_in(0x3da);
  311.   port_out(Wert & 0xff, 0x3c0);
  312.   h = port_in(0x3c1);
  313.   i = port_in(0x3da);
  314.   port_out(32, 0x3c0);
  315.   port_outw((h << 8) + 78, PortNr);
  316. }
  317.  
  318. WORD V_GetColorKey()
  319. {
  320.   return ColorKey;
  321. }
  322.  
  323. void V_EnableFieldReplication()
  324. {
  325.   port_out(80, PortNr);
  326.   port_out(port_in(PortNr + 1) | 8, PortNr+1);
  327.   port_out(32, PortNr);
  328.   port_out(port_in(PortNr + 1) | 4, PortNr+1);
  329. }
  330.  
  331. void V_DisableFieldReplication()
  332. {
  333.   port_out(80, PortNr);
  334.   port_out(port_in(PortNr + 1) & 0xf7, PortNr+1);
  335.   port_out(32, PortNr);
  336.   port_out(port_in(PortNr + 1) & 0xfb, PortNr+1);
  337. }
  338.  
  339. void V_FreezeVideo()
  340. {
  341.   WORD h, i;
  342.  
  343.   port_out(32, PortNr);
  344.   h = port_in(PortNr + 1);
  345.   i = 0;
  346.   do {
  347.     port_out(32, PortNr);
  348.     h = port_in(PortNr + 1);
  349.     port_out(h & 0xfe, PortNr + 1);
  350.     i++;
  351.   } while (((h & 1) != 0) && (i != 0xffffL));
  352.   Freeze = TRUE;
  353. }
  354.  
  355. void V_UnfreezeVideo()
  356. {
  357.   port_out(32, PortNr);
  358.   port_out(port_in(PortNr + 1) | 1, PortNr+1);
  359.   Freeze = FALSE;
  360. }
  361.  
  362. BOOL V_Freeze()
  363. {
  364.   return Freeze;
  365. }
  366.  
  367. void V_SetWriteProtectMask(WORD Wert)
  368. {
  369.   WriteProtectMask = Wert;
  370.   port_outw((Wert << 8) + 7, PortNr);
  371.   port_outw((Wert & 0xff) + 8, PortNr);
  372. }
  373.  
  374. WORD V_GetWriteProtectMask()
  375. {
  376.   return WriteProtectMask;
  377. }
  378.  
  379. void V_SetVideoSource(WORD Wert)
  380. {
  381.   VideoSource = Wert & 3;
  382.   if (Wert == 0) {
  383.     V_wr_i2c(0x8a, 10, 2);
  384.     return;
  385.   }
  386.   if (Wert == 1) {
  387.     V_wr_i2c(0x8a, 10, 42);
  388.     return;
  389.   }
  390.   if (Wert == 2)
  391.     V_wr_i2c(0x8a, 10, 18);
  392.   else
  393.     V_wr_i2c(0x8a, 10, 10);
  394. }
  395.  
  396. WORD V_GetVideoSource()
  397. {
  398.   return VideoSource;
  399. }
  400.  
  401. void V_SetInputFormat(WORD Wert)
  402. {
  403.   Wert &= 1;
  404.   InputFormat = Wert;
  405.   if (Wert == 1) {
  406.     V_wr_i2c(0x8a, 6, 34);
  407.     V_wr_i2c(0x8a, 8, 119);
  408.   } else {
  409.     V_wr_i2c(0x8a, 6, 50);
  410.     V_wr_i2c(0x8a, 8, 56);
  411.   }
  412.   UpdateVideoWindow();
  413. }
  414.  
  415. WORD V_GetInputFormat()
  416. {
  417.   return InputFormat;
  418. }
  419.  
  420. void V_SetColor(WORD x, WORD Wert)
  421. {
  422.   if (x > 6) return;
  423.   if (x == 3) {
  424.     Wert = (Wert << 2) + 128;
  425.     V_wr_i2c(0x8a, 7, Wert);
  426.   } else
  427.     V_wr_i2c(0x88, x, Wert);
  428.   FarbEinstellung[x] = Wert;
  429. }
  430.  
  431. WORD V_GetColor(WORD x)
  432. {
  433.   if (x > 6) return 0;
  434.   return (FarbEinstellung[x]);
  435. }
  436.  
  437. void V_SetVideoAddress(WORD Wert)
  438. {
  439.   Wert &= 15;
  440.   port_out(6, PortNr);
  441.   port_out((port_in(PortNr + 1) & 0xf0) | Wert, PortNr+1);
  442.  
  443. #ifdef MemAccess
  444. #ifdef linux
  445.   munmap((caddr_t)(0x00100000*VideoAddress), 0x00100000);
  446.   VidMem = (BYTE *) mmap(
  447.     (caddr_t) (0x00100000*Wert),           /* memoryaddress         */
  448.     0x00100000,                            /* size                  */
  449.     PROT_READ|PROT_WRITE,                  /* allow read and write  */
  450.     MAP_SHARED|MAP_FIXED,                  /* fix memory to address */
  451.     Mem_descriptor,
  452.     (0x00100000*Wert)                      /* same as first param.  */
  453.   );
  454.   if (VidMem == NULL){
  455.     fprintf(stderr,"can't access memory.\n");
  456.     exit(-1);
  457.   }
  458. #else  /* address for DJGPP, mapped at 0xe0000000+VideoAddress */
  459.   VidMem = (BYTE *)(0xe0000000 + (0x00100000*Wert));
  460. #endif
  461.   VideoAddress = Wert;
  462. #else
  463.   VideoAddress = 0;
  464.   VidMem = NULL;
  465. #endif
  466. }
  467.  
  468. void V_SetCaptureAddress(WORD x, WORD y)
  469. {
  470.   x <<= 1;
  471.   port_outw(((x << 8) & 0xf7) + 42, PortNr);
  472.   x &= 0x700;
  473.   y = (y & 0xfffeL) << 2;
  474.   port_outw((x | (y << 8)) + 43, PortNr);
  475.   port_outw((y & 0xff00L) + 44, PortNr);
  476. }
  477.  
  478. WORD V_Initialize(char *name, INT clear)
  479. {
  480.   WORD Config[0x2c] = {
  481.     0x002b, 0x1001, 0x1906, 0xff07, 0xff08, 0x0009, 0x2612, 0x1013, 0x3318,
  482.     0x2120, 0x0d21, 0x1022, 0x0023, 0x2224, 0x0025, 0xb026, 0x0227, 0x2028,
  483.     0x0229, 0x002a, 0x002b, 0x002c, 0x202d, 0x202e, 0x202f, 0x0630, 0x9438,
  484.     0xa340, 0x0041, 0x0042, 0x2043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048,
  485.     0xf849, 0xe04a, 0x114b, 0x124c, 0x004d, 0x004e, 0x004f, 0x0050
  486.   };
  487.  
  488.   BYTE Config2[28] = {
  489.     0x64, 0x35, 0x0a, 0xf8, 0xcd, 0xfe, 0x22, 0x00, 0x77, 0xe0, 0x02, 0x00,
  490.     0x3f, 0x12, 0x16, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3f, 0x00,
  491.     0x81, 0x10, 0x00, 0x00
  492.   };
  493.  
  494.   INT  i, hsync = 0, vsync = 0;
  495.   WORD v, w;
  496.   FILE *cfgfile;
  497.   char line[255], *tmp, FName[250];
  498.   int  Volume;
  499.  
  500.   Volume = 0;
  501.   VideoSource = 0;
  502.   InputFormat = 0;
  503.   HorizontalZoom = 0;
  504.   VerticalZoom = 0;
  505.   VideoAddress = 13;
  506.   PortNr = 0xad6;
  507.   if (*name == 0) strcpy(FName,"video.cfg");
  508.   else strcpy(FName,name);
  509.   if ((cfgfile = fopen(FName,"r")) != NULL) {
  510.   /* read config file */
  511.     i = 1;
  512.     while (!feof(cfgfile)){
  513.       fgets(line,250,cfgfile);
  514.       if (*line != '#'){
  515.         tmp = line;
  516.         while ((*tmp != '=') && (*tmp != 0)) tmp++;
  517.         if (*tmp == 0){
  518.           fprintf(stderr,"error in file %s, line %i\n", FName, i);
  519.           exit(-1);
  520.         }
  521.         *tmp++ = 0;
  522.         if (!strcmp(line,"Port")){
  523.           if ((PortNr = atoi(tmp)) == 0){
  524.             fprintf(stderr,"wrong port number in file %s, line %i\n", FName,i);
  525.             exit(-1);
  526.           }
  527.         }
  528.         else
  529.         if (!strcmp(line,"Memory")){
  530.           if ((VideoAddress = atoi(tmp)) == 0){
  531.             fprintf(stderr,"wrong video address in file %s, line %i\n",FName,i);
  532.             exit(-1);
  533.           }
  534.         }
  535.         else
  536.         if (!strcmp(line,"DisplayWindowX")) Einstellung[0] = atoi(tmp);
  537.         else
  538.         if (!strcmp(line,"DisplayWindowY")) Einstellung[1] = atoi(tmp);
  539.         else
  540.         if (!strcmp(line,"DisplayAddressX")) Einstellung[2] = atoi(tmp);
  541.         else
  542.         if (!strcmp(line,"DisplayAddressY")) Einstellung[3] = atoi(tmp);
  543.         else
  544.         if (!strcmp(line,"ShiftClockStart")) Einstellung[4] = atoi(tmp);
  545.         else
  546.         if (!strcmp(line,"PaletteSkew")) Einstellung[5] = atoi(tmp);
  547.         else
  548.         if (!strcmp(line,"HorizontalSync")){
  549.           if (*tmp == '+') hsync = 1;
  550.           else if (*tmp != '-'){
  551.             fprintf(stderr,"'+' or '-' for horizontal sync expected\n");
  552.             fprintf(stderr,"file %s, line %i\n",FName,i);
  553.             exit(-1);
  554.           }
  555.         }
  556.         else
  557.         if (!strcmp(line,"VerticalSync")){
  558.           if (*tmp == '+') vsync = 1;
  559.           else if (*tmp != '-'){
  560.             fprintf(stderr,"'+' or '-' for vertical sync expected\n");
  561.             fprintf(stderr,"file %s, line %i\n",FName,i);
  562.             exit(-1);
  563.           }
  564.         }
  565.         else
  566.         if (!strcmp(line,"Brightness")) FarbEinstellung[0] = atoi(tmp);
  567.         else
  568.         if (!strcmp(line,"Saturation")) FarbEinstellung[1] = atoi(tmp);
  569.         else
  570.         if (!strcmp(line,"Contrast")) FarbEinstellung[2] = atoi(tmp);
  571.         else
  572.         if (!strcmp(line,"Hue")) FarbEinstellung[3] = atoi(tmp);
  573.         else
  574.         if (!strcmp(line,"Red")) FarbEinstellung[4] = atoi(tmp);
  575.         else
  576.         if (!strcmp(line,"Green")) FarbEinstellung[5] = atoi(tmp);
  577.         else
  578.         if (!strcmp(line,"Blue")) FarbEinstellung[6] = atoi(tmp);
  579.         else
  580.         if (!strcmp(line,"VideoSource")) VideoSource = atoi(tmp);
  581.         else
  582.         if (!strcmp(line,"InputFormat")) InputFormat = atoi(tmp);
  583.         else
  584.         if (!strcmp(line,"HorizontalZoom")) HorizontalZoom = atoi(tmp);
  585.         else
  586.         if (!strcmp(line,"VerticalZoom")) VerticalZoom = atoi(tmp);
  587.         else
  588.         if (!strcmp(line,"Volume")) Volume = atoi(tmp);
  589.         else{
  590.           fprintf(stderr,"unknown command in file %s, line %i\n",FName,i);
  591.           fprintf(stderr,"%s=%s\n",line,tmp);
  592.           exit(-1);
  593.         }
  594.       } /* no comment */
  595.       i++;
  596.     } /* while line in file */
  597.   } /* file found */
  598.   Freeze = FALSE;
  599.   Flag_HiColor = FALSE;
  600.   Flag_TrueColor = FALSE;
  601.   Flag_3_4 = FALSE;
  602.   ColorKey = 0;
  603.  
  604. #ifdef linux
  605.   if (iopl(3) != 0){
  606.     fprintf(stderr,"can't open ports.\n");
  607.     exit(-1);
  608.   }
  609. #ifdef MemAccess
  610.   if ((Mem_descriptor = open("/dev/mem", O_RDWR)) < 0){
  611.     fprintf(stderr,"can't open /dev/mem.\n");
  612.     exit(-1);
  613.   }
  614.   VidMem = (BYTE *) mmap(
  615.     (caddr_t) (0x00100000*VideoAddress),   /* memoryaddress         */
  616.     0x00100000,                            /* size                  */
  617.     PROT_READ|PROT_WRITE,                  /* allow read and write  */
  618.     MAP_SHARED|MAP_FIXED,                  /* fix memory to address */
  619.     Mem_descriptor,
  620.     (0x00100000*VideoAddress)              /* same as first param.  */
  621.   );
  622.   if (VidMem == NULL){
  623.     fprintf(stderr,"can't access memory.\n");
  624.     exit(-1);
  625.   }
  626. #else
  627.   VidMem = NULL;
  628.   Mem_descriptor = -1;
  629. #endif
  630. #else  /* address for DJGPP, mapped at 0xe0000000+VideoAddress */
  631. #ifdef MemAccess
  632.   VidMem = (BYTE *)(0xe0000000 + (0x00100000*VideoAddress));
  633. #else
  634.   VidMem = 0;
  635. #endif
  636. #endif
  637.  
  638.   port_out(PortNr & 0xff, PortNr);
  639.   port_outw(0x3ff, PortNr);
  640.   port_outw(0x7a4e, PortNr);
  641.   port_in(PortNr+1);
  642.   /*
  643.   if (port_in(PortNr+1) != 122) {
  644.     fprintf(stderr,"videocard not found.\n");
  645.     exit(-1);
  646.   }
  647.   */
  648.   for (i = 1; (WORD)i <= Config[0]; i++) {
  649.     port_outw(Config[i], PortNr);
  650.   }
  651.   port_outw(((Einstellung[4]) << 8) + 76, PortNr);
  652.   port_out(64, PortNr);
  653.   port_out((port_in(PortNr + 1) & 63) | (Einstellung[5] << 6), PortNr+1);
  654.   port_outw(0x13, PortNr);
  655.   port_outw(((Einstellung[6]) << 8) + 18, PortNr);
  656.   port_outw(0x113, PortNr);
  657.   port_outw(((Einstellung[7]) << 8) + 18, PortNr);
  658.   for (i = 0; i <= 11; i++) {
  659.     V_wr_i2c(0x8a, i, Config2[i]);
  660.   }
  661.   for (i = 0; i <= 15; i++) {
  662.     V_wr_i2c(0x88, i, Config2[i + 12]);
  663.   }
  664.   V_SetVolume(0);
  665.   for (i=0; i<=6; i++)
  666.     V_SetColor(i, FarbEinstellung[i]);
  667.  
  668.   V_wr_i2c(0xbe, 1, 0);
  669.   V_wr_i2c(0xbe, 2, 0);
  670.   V_wr_i2c(0xbe, 3, 5);
  671.   V_wr_i2c(0x8a, 9, 0xe0 | (Einstellung[8] & 4));
  672.   V_SetInputFormat(InputFormat);
  673.   V_SetVideoSource(VideoSource);
  674.   if ((Einstellung[8] & 2) > 0) {
  675.     port_out(80, PortNr);
  676.     port_out(port_in(PortNr + 1) | 1, PortNr+1);
  677.   }
  678.   port_out(77, PortNr);
  679.   port_out(((Einstellung[8] >> 3) & 1) | (Einstellung[8] & 48), PortNr+1);
  680.   if ((Einstellung[8] & 8) > 0)
  681.     Flag_3_4 = TRUE;
  682.   else
  683.     Flag_3_4 = FALSE;
  684.   port_out(34, PortNr);
  685.   v = port_in(PortNr + 1);
  686.   port_out(35, PortNr);
  687.   v += (((WORD) port_in(PortNr + 1)) << 8) & 0x300;
  688.   port_out(38, PortNr);
  689.   w = port_in(PortNr + 1);
  690.   port_out(39, PortNr);
  691.   w += (((WORD) port_in(PortNr + 1)) << 8) & 0x300;
  692.   if (w == v)
  693.     return 0;
  694.   port_out(36, PortNr);
  695.   v = port_in(PortNr + 1);
  696.   port_out(37, PortNr);
  697.   v += (((WORD) port_in(PortNr + 1)) << 8) & 0x300;
  698.   port_out(40, PortNr);
  699.   w = port_in(PortNr + 1);
  700.   port_out(41, PortNr);
  701.   w += (((WORD) port_in(PortNr + 1)) << 8) & 0x300;
  702.   if (w == v)
  703.     return 0;
  704.   V_SetVideoAddress(VideoAddress);
  705.   V_SetCaptureAddress(0, 0);
  706.   V_HorizontalZoom(HorizontalZoom);
  707.   V_VerticalZoom(VerticalZoom);
  708.   V_SetSkewFactor(7,hsync);
  709.   V_SetSkewFactor(8,vsync);
  710. #ifdef MemAccess
  711.   if (clear == 1){
  712.     V_FreezeVideo();
  713.     memset(VidMem, 0, 0x100000);
  714.     V_UnfreezeVideo();
  715.   }
  716. #endif
  717.   V_SetVolume(Volume);
  718.   return 1;
  719. }
  720.  
  721. void V_Exit(){
  722. #ifdef MemAccess
  723. #ifdef linux
  724.   munmap((caddr_t)(0x00100000*VideoAddress), 0x00100000);
  725.   close(Mem_descriptor);
  726. #endif
  727. #endif
  728.   V_DisableVideo();
  729.   V_SetVolume(0);
  730. }
  731.  
  732. void V_HorizontalZoom(WORD Wert)
  733. {
  734.   WORD h;
  735.  
  736.   Wert &= 3;
  737.   HorizontalZoom = Wert;
  738.   port_out(77, PortNr);
  739.   port_out((port_in(PortNr + 1) & 0xfc) | Wert, PortNr+1);
  740.   h = (Wert << 1) + Einstellung[4];
  741.   port_outw((h << 8) + 76, PortNr);
  742. }
  743.  
  744. void V_VerticalZoom(WORD Wert)
  745. {
  746.   Wert &= 3;
  747.   VerticalZoom = Wert;
  748.   port_out(77, PortNr);
  749.   port_out((port_in(PortNr + 1) & 0xf3) + (Wert << 2), PortNr+1);
  750. }
  751.  
  752. void V_CreateWindow(INT x, INT y, WORD b, WORD h)
  753. {
  754.   x = x-26;  /* -26 bei meinen Einstellungen */
  755.   WindowPosX = x;
  756.   WindowPosY = y;
  757.   WindowSizeX = b;
  758.   WindowSizeY = h;
  759.   setdisplaywindow(x, y, b, h);
  760.   UpdateVideoWindow();
  761. }
  762.  
  763. void V_SetWindowPosition(INT x, INT y)
  764. {
  765.   WindowPosX = x-26;
  766.   WindowPosY = y;
  767.   setdisplayposition(x, y);
  768.   UpdateVideoWindow();
  769. }
  770.  
  771. void V_SetWindowSize(WORD b, WORD h)
  772. {
  773.   WindowSizeX = b;
  774.   WindowSizeY = h;
  775.   scale_video(b, h);
  776.   UpdateVideoWindow();
  777. }
  778.  
  779. void V_SetRegister(WORD Nr, WORD Wert)
  780. {
  781.   WORD h;
  782.  
  783.   if (Nr > 255) {
  784.     V_wr_i2c(Wert >> 8, Wert & 0xff, Nr & 255);
  785.     return;
  786.   }
  787.   h = port_in(PortNr);
  788.   port_out(Nr, PortNr);
  789.   port_out(Wert, PortNr + 1);
  790.   port_out(h, PortNr);
  791. }
  792.  
  793. WORD V_GetRegister(WORD Nr)
  794. {
  795.   WORD Result;
  796.   BYTE h;
  797.  
  798.   if ((Nr & 0xff00L) != 0)
  799.     return 0xff;
  800.   h = port_in(PortNr);
  801.   port_out(Nr, PortNr);
  802.   Result = port_in(PortNr + 1);
  803.   port_out(h, PortNr);
  804.   return Result;
  805. }
  806.  
  807. void V_SetSkewFactor(WORD x, WORD Wert)
  808. {
  809.   WORD a;
  810.  
  811.   switch (x) {
  812.  
  813.   case 0:
  814.     Einstellung[0] = (Einstellung[0] & 0xf800) | (Wert & 0x07ff);
  815.     break;
  816.  
  817.   case 1:
  818.     Einstellung[1] = (Einstellung[1] & 0xfc00) | (Wert & 0x03ff);
  819.     break;
  820.  
  821.   case 2:
  822.     Einstellung[2] = (Einstellung[2] & 0xfc00) | (Wert & 0x03ff);
  823.     break;
  824.  
  825.   case 3:
  826.     Einstellung[3] = (Einstellung[3] & 0xfe00) | (Wert & 0x01ff);
  827.     break;
  828.  
  829.   case 4:
  830.     Einstellung[4] = (Einstellung[4] & 0xff80) | (Wert & 0x007f);
  831.     port_out(76, PortNr);
  832.     port_out(Einstellung[4] & 0x00ff, PortNr+1);
  833.     break;
  834.  
  835.   case 5:
  836.     Einstellung[5] = (Einstellung[5] & 0xfffc) | (Wert & 0x0003);
  837.     port_out(64, PortNr);
  838.     a = (port_in(PortNr + 1) & 0x3f) | (Einstellung[5] << 6);
  839.     port_out(a & 0xff, PortNr+1);
  840.     break;
  841.  
  842.   case 6:
  843.     Einstellung[6] = (Einstellung[6] & 0xfc00) | (Wert & 0x03ff);
  844.     port_out(19, PortNr);
  845.     port_out(1, PortNr + 1);
  846.     port_out(18, PortNr);
  847.     port_out(Einstellung[6] >> 8, PortNr+1);
  848.     port_out(19, PortNr);
  849.     port_out(0, PortNr + 1);
  850.     port_out(18, PortNr);
  851.     port_out(Einstellung[6] & 0xff, PortNr+1);
  852.     break;
  853.  
  854.   case 7:
  855.     if (Wert == 0) {
  856.       a = V_GetRegister(77) & 0xef;
  857.       Einstellung[4] &= 0xffef;
  858.     } else {
  859.       a = V_GetRegister(77) | 16;
  860.       Einstellung[4] |= 16;
  861.     }
  862.     V_SetRegister(77, a);
  863.     break;
  864.  
  865.   case 8:
  866.     if (Wert == 0) {
  867.       a = V_GetRegister(77) & 0xdf;
  868.       Einstellung[4] &= 0xffdf;
  869.     } else {
  870.       a = V_GetRegister(77) | 32;
  871.       Einstellung[4] |= 32;
  872.     }
  873.     V_SetRegister(77, a);
  874.     break;
  875.  
  876.   case 9:
  877.     if (Wert == 0) {
  878.       Flag_HiColor = TRUE;
  879.       Einstellung[9] |= 1;
  880.     } else {
  881.       Flag_HiColor = FALSE;
  882.       Einstellung[9] &= 0xfffe;
  883.     }
  884.     break;
  885.  
  886.   case 10:
  887.     if (Wert == 0) {
  888.       Flag_TrueColor = TRUE;
  889.       Einstellung[9] |= 4;
  890.     } else {
  891.       Flag_TrueColor = FALSE;
  892.       Einstellung[9] &= 0xfffb;
  893.     }
  894.     break;
  895.  
  896.   case 11:
  897.     if (Wert == 0) {
  898.       V_wr_i2c(0x8a, 9, 0xe4);
  899.       Einstellung[8] |= 4;
  900.     } else {
  901.       V_wr_i2c(0x8a, 9, 0xe0);
  902.       Einstellung[8] &= 0xfffb;
  903.     }
  904.     break;
  905.  
  906.   case 12:
  907.     if (Wert == 0){
  908.       Einstellung[8] |= 8;
  909.       Flag_3_4 = TRUE;
  910.     }
  911.     else{
  912.       Einstellung[8] &= 0xfff7;
  913.       Flag_3_4 = FALSE;
  914.     }
  915.     V_HorizontalZoom(HorizontalZoom);
  916.     break;
  917.   }
  918.   UpdateVideoWindow();
  919. }
  920.  
  921. WORD V_GetSkewFactor(WORD x)
  922. {
  923.   if (x > 12) return 0;
  924.   switch (x) {
  925.  
  926.   case 0:
  927.     return Einstellung[0] & 0x07ff;
  928.     break;
  929.  
  930.   case 1:
  931.     return Einstellung[1] & 0x03ff;
  932.     break;
  933.  
  934.   case 2:
  935.     return Einstellung[2] & 0x03ff;
  936.     break;
  937.  
  938.   case 3:
  939.     return Einstellung[3] & 0x01ff;
  940.     break;
  941.  
  942.   case 4:
  943.     return Einstellung[4] & 0x007f;
  944.     break;
  945.  
  946.   case 5:
  947.     return Einstellung[5] & 0x0003;
  948.     break;
  949.  
  950.   case 6:
  951.     return Einstellung[6] & 0x03ff;
  952.     break;
  953.  
  954.   case 7:
  955.     if ((Einstellung[4] & 16) > 0) return 1;
  956.     else                           return 0;
  957.     break;
  958.  
  959.   case 8:
  960.     if ((Einstellung[4] & 32) > 0) return 1;
  961.     else                           return 0;
  962.     break;
  963.  
  964.   case 9:
  965.     if ((Einstellung[9] & 1) > 0) return 1;
  966.     else                          return 0;
  967.     break;
  968.  
  969.   case 10:
  970.     if ((Einstellung[9] & 4) > 0) return 1;
  971.     else                          return 0;
  972.     break;
  973.  
  974.   case 11:
  975.     if ((Einstellung[8] & 4) > 0) return 1;
  976.     else                          return 0;
  977.     break;
  978.  
  979.   case 12:
  980.     if ((Einstellung[8] & 8) > 0) return 1;
  981.     else                          return 0;
  982.     break;
  983.   }
  984.   return 0;
  985. }
  986.  
  987. void V_EnableInterlace()
  988. {
  989.   port_out(80, PortNr);
  990.   port_out(port_in(PortNr+1) | 1, PortNr+1);
  991.   Einstellung[8] |= 2;
  992. }
  993.  
  994. void V_DisableInterlace()
  995. {
  996.   port_out(80, PortNr);
  997.   port_out(port_in(PortNr+1) & 0xfe, PortNr+1);
  998.   Einstellung[8] &= 0xfffd;
  999. }
  1000.  
  1001. void V_WaitGrabbed()
  1002. {
  1003.   port_out(9, PortNr);
  1004.   while ((port_in(PortNr+1) & 8) > 0) {}
  1005.   while ((port_in(PortNr+1) & 8) == 0) {}
  1006.   while ((port_in(PortNr+1) & 8) > 0) {}
  1007.   while ((port_in(PortNr+1) & 8) == 0) {}
  1008.   while ((port_in(PortNr+1) & 8) > 0) {}
  1009. }
  1010.  
  1011. /*----------------------------------------------------------------------*/
  1012.